﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="FindAllHospital1.aspx.cs"
    Inherits="HZ.Supervise.Web.Common.FindAllHospital1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>选择医院</title>
    <link rel="stylesheet" type="text/css" href="../style/global.css" />

    <script type="text/javascript" src="../scripts/jquery.js"></script>

    <script type="text/javascript" src="../scripts/mainlistSelect.js"></script>

    <script type="text/javascript" src="../scripts/Common.js"></script>

    <script type="text/javascript" src="../scripts/global.js"></script>

    <script type="text/javascript" language="javascript">
        function addHosInfo() {
            var hosInfo;
            hosInfo = document.getElementById("HosName").value + "|" + document.getElementById("HosId").value;
            window.opener.insertHosInfo(hosInfo);
            window.close();
        }

        function chkAllCompany() {
            CheckBoxList = document.getElementsByName("chkHos");
            for (var i = 0; i < CheckBoxList.length; i++) {
                CheckBoxList[i].checked = true;
            }
        }


        function insertCompanyInfo() {

            //            if (!checkSelectedItems('gvwHosList', '请至少选择一项！')) {
            //                return false;
            //            }

            //CheckBoxList = document.getElementsByName("chkHos");

            var hidCheck = document.getElementById("hidCheck"); //得到隐藏控件

            if (hidCheck.value.length <= 0) {
                alert("请至少选择一项！");
                return false;
            }
            var hidCheckArray = hidCheck.value.split(',');

            var resultValue = "";
            var hosNames = "";
            var hosIDs = "";

            var listbox = document.getElementById("ListHos");
            if (listbox.options.length >= 5) {
                alert("医院对比数量已经满，不能再添加！");
                return false;
            }
            var istrue = 0;

            //            for (var i = 0; i < CheckBoxList.length; i++) {
            //                if (CheckBoxList[i].checked) {
            //                    resultValue = CheckBoxList[i].value;
            //                    tempId = resultValue.split("|")[0];
            //                    tempName = resultValue.split("|")[1];

            //                    hosIDs = hosIDs + tempId + ",";
            //                    hosNames = hosNames + tempName + ",";

            //                    istrue++;
            //                }
            //            }

            for (var i = 0; i < hidCheckArray.length - 1; i++) {

                resultValue = hidCheckArray[i];
                tempId = resultValue.split("|")[0];
                tempName = resultValue.split("|")[1];

                hosIDs = hosIDs + tempId + ",";
                hosNames = hosNames + tempName + ",";

                istrue++;
            }


            if (istrue > 5 && (istrue + listbox.options.length) >= 5) {
                alert("医院对比数量不得超过" + (5 - listbox.options.length) + "家");
                return false;
            }

            var listbox = document.getElementById("ListHos");
            var hosID = document.getElementById("HosId");
            var hosName = document.getElementById("HosName");
            var arrayName = hosNames.split(',');
            var arrayID = hosIDs.split(',');
            //var isUnique = true;
            var temp;

            listbox.length = 0;

            for (m = 0; m < arrayID.length - 1; m++) {

                var listItem = document.createElement("OPTION");

                listItem.text = arrayName[m];

                listItem.value = arrayID[m];

                listbox.add(listItem);
            }
            //                for (m = 0; m < arrayID.length; m++) {
            //                    for (n = 0; n < listbox.options.length; n++) {
            //                        if (arrayID[m] == listbox.options[n].value) {
            //                            //                        isUnique = false;
            //                            //temp = arrayName[m];
            //                            break;
            //                        }

            //                       
            //                    }
            //}

            //            if (isUnique == true) {
            //                for (var i = 0; i < arrayName.length; i++) {
            //                    if (!isNull(arrayName[i])) {
            //                        var listItem = document.createElement("OPTION");
            //                        listItem.text = arrayName[i];
            //                        listItem.value = arrayID[i];
            //                        listbox.add(listItem);
            //                    }
            //                }
            //            }
            //            else {
            //                alert("医院" + temp + "已经存在！");
            //                return;
            //            }

            hosID.value = "";
            hosName.value = "";
            for (j = listbox.options.length - 1; j >= 0; j--) {
                hosID.value = hosID.value + listbox[j].value + ",";
                hosName.value = hosName.value + listbox[j].text + ",";
            }
        }

        function deleteCompany() {
            if (!isNull(checkSelected())) {
                var info = checkSelected();
                alert(info);
                return;
            }
            var listbox = document.getElementById("ListHos");
            var companyID = document.getElementById("HosId");
            var companyName = document.getElementById("HosName");
            companyID.value = "";
            companyName.value = "";

            var selectindex = listbox.selectedIndex;
            var hidCheck = document.getElementById("hidCheck");
            if (hidCheck.value.indexOf(listbox[selectindex].value + "|" + listbox[selectindex].text + ",") >= 0) {


                hidCheck.value = hidCheck.value.replace(listbox[selectindex].value + "|" + listbox[selectindex].text + ",", "");

            }
            listbox.remove(selectindex);


            for (j = listbox.options.length - 1; j >= 0; j--) {
                companyID.value = companyID.value + listbox[j].value + ",";
                companyName.value = companyName.value + listbox[j].text + ",";
            }
        }

        function checkSelected() {
            var info;
            var listbox = document.getElementById("ListHos");
            var selectindex = listbox.selectedIndex;
            if (selectindex < 0) {
                info = "请选择要删除的医院！";
                return info;
            }
            return "";
        }

        function isNull(str) {
            var re = /^[ \s]*$/;
            return re.test(str);
        }


        //add by 曹鹏 2011年4月28日17:29:13 实现Gridview点击下一页时，记录用户的前面所选项
        function InsertChecked(owner_obj) {

            var hidCheck = document.getElementById("hidCheck"); //得到隐藏控件

            var HosName = document.getElementById("HosName");
            var HosId = document.getElementById("HosId");

            if (owner_obj.checked) {

                if (hidCheck.value.split(",").length > 5) {

                    alert("选择的医院数量不能超过5家！");

                    owner_obj.checked = false;

                    return;

                }
                else {

                    hidCheck.value += owner_obj.value + ","; //如果用户选中checkbox，加到隐藏控件value属性中，值用“，”隔开
                    HosName.value += owner_obj.value.split("|")[1] + ",";
                    HosId.value += owner_obj.value.split("|")[0] + ",";
                }
            }

            else {

                hidCheck.value = hidCheck.value.replace(owner_obj.value + ",", ""); //如果用户取消选中，将checkbox对应值从隐藏控件value之中移除
                HosName.value = HosName.value.replace(owner_obj.value.split("|")[1] + ",", "");
                HosId.value = HosId.value.replace(owner_obj.value.split("|")[0] + ",", "");
            }

        }

        function checkRe() {//当gridview翻页时

            var hidCheck = document.getElementById("hidCheck"); //得到隐藏控件

            var CheckBoxList = document.getElementsByName("chkHos"); //得到gridview中checkbox集合

            for (var i = 0; i < CheckBoxList.length; i++) {//遍历

                if (hidCheck.value.indexOf(CheckBoxList[i].value) >= 0) {//判断是否存在

                    CheckBoxList[i].checked = true; //选中

                }

            }

        }
        function recover() {

            var hidCheck = document.getElementById("hidCheck"); //得到隐藏控件

            var listbox = document.getElementById("ListHos");

            listbox.length = 0;

            var array = hidCheck.value.split(",");

            for (var m = 0; m < array.length - 1; m++) {

                var listItem = document.createElement("OPTION");

                listItem.text = array[m].split("|")[1];

                listItem.value = array[m].split("|")[0];

                listbox.add(listItem);

            }
        }
        
    //by张雄  回车查询 2011.5.10
    function document.onkeydown() {
        var btn = document.getElementById('<%=btnQery.ClientID %>');
        if(event.keyCode == 13)
        {
            btn.click();
            event.returnValue = false;
        }
    }
    </script>

</head>
<body>
    <form id="form1" runat="server">
    <div class="listsearch">
        <table class="querylist">
            <tr>
                <td style="width: 60px">
                    <asp:Label ID="lblQueryCondition" runat="server" Text="医院名称："></asp:Label>
                </td>
                <td align="left">
                    <asp:TextBox ID="txtHosName" runat="server"></asp:TextBox>
                </td>
                <td style="width: 60px">
                    <hz:HzButton ID="btnQery" runat="server" Text="查  询" CssClass="btn" OnClick="btnQery_Click" />
                </td>
            </tr>
        </table>
    </div>
    <table class="querylist">
        <tr>
            <td width="40%">
                选择医院：
            </td>
            <td width="20%">
                &nbsp;
            </td>
            <td width="50%">
                对比医院：
            </td>
        </tr>
        <tr>
            <td>
            <div><font color="red">提示：选择的医院数量不能超过5家！</font></div>
                <asp:GridView ID="gvwHosList" runat="server" CssClass="mainlist" AutoGenerateColumns="False"
                    DataKeyNames="HospitalID" EmptyDataText="暂无符合条件的数据！" OnDataBound="gvwHosList_DataBound">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                <%--<input id="chkAllHos" name="chkAllHos" type="checkbox" onclick="SelectAllCheckboxes(this,'gvwHosList','chkAllHos');" />--%>
                                请选择
                            </HeaderTemplate>
                            <ItemTemplate>
                                <input id="chkHos" name="chkHos" type="checkbox" <%--onclick="InsertChecked(this);chkChecked(this,'gvwHosList','chkAllHos');"--%>
                                    onclick="InsertChecked(this);" value='<%# Eval("HospitalID") %>|<%# Eval("HospitalName") %>'>
                                </input>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="医院名称">
                            <ItemTemplate>
                                <asp:Label ID="lblCoHosName" runat="server" Text='<%# Eval("HospitalName") %>'></asp:Label>
                            </ItemTemplate>
                            <ItemStyle HorizontalAlign="Left" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <webdiyer:AspNetPager ID="AspNetPager1" runat="server" NumericButtonCount="5" CustomInfoStyle="hide"
                    OnPageChanged="AspNetPager1_PageChanged">
                </webdiyer:AspNetPager>
                <input id="hidCheck" name="hidCheck" type="hidden" runat="server" />
            </td>
            <td>
                <p>
                    <input type="button" id="btnIn" value="加入对比" class="btn" onclick="insertCompanyInfo();" /></p>
                <p>
                    <input type="button" id="btnOut" value="删除对比" class="btn" onclick="deleteCompany();" /></p>
                <p>
                    <input type="button" id="btnOk" value="确认对比" class="btn" onclick="return addHosInfo();" /></p>
            </td>
            <td rowspan="2">
                <input id="HosId" type="hidden" name="HosId" runat="server" />
                <asp:ListBox ID="ListHos" runat="server" Height="300px" Width="100%"></asp:ListBox>
                <input id="HosName" type="hidden" name="HosName" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                &nbsp;
            </td>
        </tr>

        <script type='text/javascript'>
            checkRe(); recover();
        </script>

    </table>
    </form>
</body>
</html>
